package com.itcanteen.medical.patient.util;

import java.math.BigDecimal;

/**
 @author WangZhuoRan */
public class DistanceUtil {

    /**
     * 地球半径系数
     */
    private static final Double EARTH_RADIUS = 6370.996;

    /**
     * π
     */
    private static final Double PI = 3.1415926;

    /**
     * @Description 精度 保留小数位数
     */
    private static final Integer DECIMAL = 0;

    private static final Integer METER = 1000;

    /**
     * @Description 根据经纬度计算距离
     * @Author WangZhuoRan
     * @Date 2020-12-22 14:40
     * @param  longitude1 起点经度
     * @param  latitude1  起点纬度
     * @param  longitude2 终点经度
     * @param  latitude2  终点纬度
     * @return BigDecimal
     */
    public static Integer getDistance(
            Double longitude1, Double latitude1,
            Double longitude2, Double latitude2){

        double radLat1 = latitude1 * PI / 180.0;
        double radLat2 = latitude2 * PI / 180.0;

        Double radLng1 = longitude1 * PI / 180.0;
        Double radLng2 = longitude2 * PI /180.0;

        double a = radLat1 - radLat2;
        double b = radLng1 - radLng2;

        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b/2),2)));
        distance = distance * EARTH_RADIUS * METER;

        BigDecimal bg = BigDecimal.valueOf(distance);
        return bg.setScale(DECIMAL, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public static void main (String[] args) {
        //华东理工->八维宏烨  18.92
        System.out.println(">>>>>>>>>>>>>>>>>>>>>"+getDistance(121.727059, 30.866484, 121.696492, 30.870378));
    }

}
